<?php

namespace app\api\home;

use app\api\validate\GroupValidate;
use app\common\controller\Common;
use app\service\audioVideo\Rtc;
use app\service\im\Group as ServiceGroup;
use app\service\live\LiveAuthKey;
use app\api\validate\LiveAnchorValidate;
use app\api\validate\LivePlayValidate;
use app\api\model\LiveAnchor;
use app\api\model\LivePlay;

class LiveController extends Base
{

    /**
     * Notes:获取推流地址
     * User: tangfen
     * Date: 2023/12/20
     * Time: 14:02
     */
    public function getPushLiveUrl()
    {
        $params = $this->request->param();

        // 验证
        $validate = new LiveAnchorValidate;
        if (!$validate->scene('add')->check($params)) {
            return_ajax(0, $validate->getError());
        }

        $res = new LiveAuthKey();
        $data = $res->pushUrl('live', $params['stream_name']);

        $anchorId = LiveAnchor::where('stream_name', $params['stream_name'])->value('id');
        if (!empty($anchorId)) {
            //更新发起直播时间
            LiveAnchor::where('stream_name', $params['stream_name'])->update(['update_time' => time()]);
            return_ajax(1, '操作成功', $data);

        } else {

            $liveAnchoor = new LiveAnchor;
            $liveAnchoor->data($params);
            $result = $liveAnchoor->save();

            //发起直播用户也在记录里面
            LivePlay::insert(['live_anchor_id' => $liveAnchoor->id, 'uid' => $params['uid'], 'stream_name' => $params['stream_name'], 'create_time' => time(), 'update_time' => time()]);

            if ($result) {
                return_ajax(1, '操作成功', $data);
            } else {
                return_ajax(0, '操作失败', $data);
            }
        }
    }


    /**
     * Notes:获取播流地址
     * User: tangfen
     * Date: 2023/12/20
     * Time: 14:02
     */
    public function getPlayLiveUrl()
    {
        $params = $this->request->param();

        // 验证
        $validate = new LivePlayValidate;
        if (!$validate->scene('getPlayUrl')->check($params)) {
            return_ajax(0, $validate->getError());
        }
        $params['uid'] = $this->uid;
        $res = new LiveAuthKey();
        $data = $res->playUrl('live', $params['stream_name']);

        $livePlayOne = LivePlay::where(['stream_name' => $params['stream_name'], 'uid' => $params['uid']])->value('id');
        if (!empty($livePlayOne)) {

            LivePlay::where(['stream_name' => $params['stream_name'], 'uid' => $params['uid']])->update(['update_time' => time()]);
            return_ajax(1, '操作成功', $data);

        } else {
            $livePlay = new LivePlay;
            $livePlay->data($params);
            $result = $livePlay->save();

            if ($result) {
                return_ajax(1, '操作成功', $data);
            } else {
                return_ajax(0, '操作失败', $data);
            }
        }
    }


    /**
     * Notes:获取直播群在线人数
     * User: tangfen
     * Date: 2023/12/23
     * Time: 13:29
     */
    public function getOnlineMemberNum()
    {
        $params = $this->request->get();
        // 验证
        $validate = new GroupValidate;
        if (!$validate->scene('getOlineGroup')->check($params)) {
            return_ajax(0, $validate->getError());
        }
        $res = new ServiceGroup();
        $data = $res->getOnlineMemberNum($params['group_id']);

        if ($data['ActionStatus'] == 'OK') {
            return_ajax(1, '操作成功', $data);
        } else {
            return_ajax(0, '操作失败', $data);
        }
    }

    /**
     * Notes:获取直播群在线列表
     * User: tangfen
     * Date: 2023/12/23
     * Time: 13:29
     */
    public function getOnlineMembers()
    {
        $params = $this->request->get();
        // 验证
        $validate = new GroupValidate;
        if (!$validate->scene('getOlineGroup')->check($params)) {
            return_ajax(0, $validate->getError());
        }
        $res = new ServiceGroup();
        $data = $res->getOnlineMembers($params['group_id']);

        if ($data['ErrorCode'] == 10059) {
            return_ajax(0, "该功能需旗舰版，并且在 IM 控制台“群功能配置”中开通“直播群在线成员列表”功能", $data);
        }
        if ($data['ActionStatus'] == 'OK') {
            return_ajax(1, '操作成功', $data);
        } else {
            return_ajax(0, '操作失败', $data);
        }
    }


}